package com.ytz.mysecurity.controller;

import org.springframework.security.access.annotation.Secured;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 12/23  18:25
 */
@RestController
public class MyController {
    @RequestMapping("/users/username")
    public String geTuSERNAME() {
        //获取会话对象
        SecurityContext context = SecurityContextHolder.getContext();
        //获取认证对象
        Authentication authentication = context.getAuthentication();
        //获取登录用户信息
        UserDetails userDetails = (UserDetails) authentication.getPrincipal();
        return userDetails.getUsername();

    }
    @Secured("ROLE_/reportform/find")
    @GetMapping("/reportform/find")
    public String findReportForm() {
        return "查询报表";
    }
    @Secured("/salary/find")
    @GetMapping("/salary/find")
    public String findSalary() {
        return "查询工资";
    }
@Secured("/staff/find")
    @GetMapping("/staff/find")
    public String findStaff() {
        return "查询员工";
    }
}
